import instance from '../utils/request'
import { Message } from 'element-ui'

// User修改提交
async function editUser (user) {
  return await instance({
    url: 'user',
    method: 'put',
    data: user
  }).then(res => {
    return res
  }).catch(() => {
    Message.error('修改失败,服务器错误!')
  })
}
// User修改提交
async function editPwd (user) {
  return await instance({
    url: 'user/pwd',
    method: 'put',
    data: user
  }).then(res => {
    return res
  }).catch(() => {
    Message.error('修改失败,服务器错误!')
  })
}
// User添加提交
async function addUser (user) {
  return await instance({
    url: 'user',
    method: 'post',
    data: user
  }).then(res => {
    return res
  }).catch(() => {
    Message.error('修改失败,服务器错误!')
  })
}

// 得到所有学生分页
async function getUsers (page, limit, search) {
  return await instance.get('/user/getUsers?page=' + page + '&limit=' + limit + '&username=' + search).then(data => {
    return data
  }).catch(() => {
    Message.error('没有权限,请先登录')
  })
}

// 得到所有学生不符
async function getUserALl () {
  return await instance.get('/user/all/student').then(data => {
    return data
  }).catch(() => {
    Message.error('没有权限,请先登录')
  })
}

// 验证密码
async function checkPassword (userId, password) {
  return await instance.get('/user/' + userId + '/' + password).then(data => {
    return data
  }).catch(() => {
    Message.error('没有权限,请先登录')
  })
}

// 删除学生
async function deleteUser (userId) {
  return await instance.delete('/user/' + userId).then(res => {
    return res
  }).catch(() => {
    Message.error('删除失败,服务器错误!')
  })
}

// 批量删除
async function deleteAllUser (userId) {
  return await instance.delete('/user/all/' + userId).then(res => {
    return res
  }).catch(() => {
    Message.error('删除失败,服务器错误!')
  })
}

export default {
  getUsers,
  deleteUser,
  editUser,
  checkPassword,
  deleteAllUser,
  editPwd,
  getUserALl,
  addUser

}
